package com.base.library.view.systemPhotoAlbum;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Gallery;

import com.base.library.R;
import com.base.library.view.zoomImageView.ZoomImageView;

import java.util.ArrayList;

/**
 * 作者：王东一 on 2016/5/31 13:24
 **/
public class gallery_adapter extends BaseAdapter {
    private ArrayList<String> imgIdArray = new ArrayList<String>();
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private ArrayList<ZoomImageView> imageViews = new ArrayList<ZoomImageView>();
    private int width;
    private int higth;
    private Point mPoint = new Point(0, 0);//用来封装ImageView的宽和高的对象

    public gallery_adapter(Context mContext) {
        this.mContext = mContext;
        mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        width = dm.widthPixels;
        higth = dm.heightPixels;
        for (int i = 0; i < imgIdArray.size(); i++) {
            ZoomImageView z = new ZoomImageView(mContext);
            z.setLayoutParams(new Gallery.LayoutParams(width, higth));
            imageViews.add(z);
        }

    }

    @Override
    public int getCount() {
        return imgIdArray.size();
    }

    @Override
    public Object getItem(int position) {
        return imgIdArray.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        //利用NativeImageLoader类加载本地图片
        Bitmap bitmap = NativeImageLoader.getInstance().loadNativeImage(imgIdArray.get(position), mPoint, new NativeImageLoader.NativeImageCallBack() {

            @Override
            public void onImageLoader(Bitmap bitmap, String path) {
                if (bitmap != null && imageViews.get(position) != null) {
                    imageViews.get(position).setImageBitmap(bitmap);
                }
            }
        });
        if (bitmap != null) {
            imageViews.get(position).setImageBitmap(bitmap);
        } else {
            imageViews.get(position).setImageResource(R.mipmap.friends_sends_pictures_no);
        }
        return imageViews.get(position);
    }

}
